package com.hzw.saas.service.hpc.util;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

public class BeanUtil {
    public static void copyProperties(Object source, Object target) {
        BeanUtils.copyProperties(source, target);
    }

    /**
     * 拷贝Bean对象
     *
     * @param source 源对象
     * @param clazz  目标对象额Class
     * @param <T>
     * @return
     */
    public static <T> T copyProperties(Object source, Class<T> clazz) {
        Object target = null;
        try {
            target = clazz.newInstance();
            copyProperties(source, target);
            return (T) target;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }

    }

    /**
     * 拷贝List集合
     *
     * @param source 源对象集合
     * @param clazz  目标对象Class
     * @param <T>
     * @return
     */
    public static <T> List<T> copyListProperties(List source, Class<T> clazz) {
        List<T> list = new ArrayList<>();
        for (Object o : source) {
            Object target = copyProperties(o, clazz);
            list.add((T) target);
        }
        return list;
    }

    public static boolean isDigit(String str) {
        if (null == str || str.length() == 0) {
            return false;
        }

        for (int i = str.length(); --i >= 0; ) {
            int c = str.charAt(i);
            if (c < 48 || c > 57) {
                return false;
            }
        }

        return true;
    }

    public static void checkNull(Object o) {
        if (o == null) {
            throw new RuntimeException("查询失败！");
        }
    }
}
